package com.alan.wifishare.presenter;

import com.alan.libapi.business.dao.CallbackListener;
import com.alan.libapi.model.business.SharedWifiInfo;
import com.alan.wifishare.app.GlobalField;
import com.alan.wifishare.view.iview.IVMainShareFM;

import java.util.ArrayList;
import java.util.List;

/**
 * @className: MainSharePS
 * @describtion: MainSharePS
 * @author: AlanWang
 * @email: 850267876@qq.com
 * @time: 2016/4/20 18:46
 */
public class MainSharePS {

    private List<SharedWifiInfo> sharedWifiInfoList = new ArrayList<>();
    private IVMainShareFM ivMainShareFM;

    public MainSharePS(IVMainShareFM ivMainShareFM) {
        this.ivMainShareFM = ivMainShareFM;
    }

    /**
     * 获取我分享的wifi列表
     */
    public void getMySharedWifiList(){
        ivMainShareFM.showProgress();
        GlobalField.businessHttp.getMySharedWifiList(GlobalField.userSession, new CallbackListener<List<SharedWifiInfo>>() {
            @Override
            public void onSuccess(final List<SharedWifiInfo> result) {
                GlobalField.handler.post(new Runnable() {
                    @Override
                    public void run() {
                        sharedWifiInfoList = result;
                        ivMainShareFM.hideProgress();
                        ivMainShareFM.showSharedWifiList(sharedWifiInfoList);
                    }
                });
            }

            @Override
            public void onFailure(int errorCode, final String msg) {
                GlobalField.handler.post(new Runnable() {
                    @Override
                    public void run() {
                        ivMainShareFM.hideProgress();
                        ivMainShareFM.toastMsg(msg);
                    }
                });
            }
        });
    }

    /**
     * 删除分享的wifi
     * @param sharedWifiInfo
     */
    public void deleteMySharedWifi(final SharedWifiInfo sharedWifiInfo){
        ivMainShareFM.showProgress();
        GlobalField.businessHttp.postDeleteWifi(GlobalField.userSession, sharedWifiInfo.getWifiId(), new CallbackListener<Void>() {
            @Override
            public void onSuccess(Void result) {
                GlobalField.handler.post(new Runnable() {
                    @Override
                    public void run() {
                        ivMainShareFM.hideProgress();
                        sharedWifiInfoList.remove(sharedWifiInfo);//移除删除成功的wifi
                        ivMainShareFM.showSharedWifiList(sharedWifiInfoList);//更新界面
                    }
                });
            }

            @Override
            public void onFailure(int errorCode, final String msg) {
                GlobalField.handler.post(new Runnable() {
                    @Override
                    public void run() {
                        ivMainShareFM.hideProgress();
                        ivMainShareFM.toastMsg(msg);
                    }
                });
            }
        });
    }

}
